package com.shop.cereshop.app.dl.logic.service.Impl;

import com.shop.cereshop.app.dl.logic.service.UpLoadService;
import com.shop.cereshop.app.dl.logic.utils.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author: 况鑫威
 * @Date: 2021/4/18
 * @Time: 14:35
 * @ClassName: UpLoadServiceImpl
 * @Description: com.ncxhr.logic.service.iml
 */
@Service
public class UpLoadServiceImpl implements UpLoadService {
    @Value("${upload.IP}")
    private String IP;
    @Value("${upload.img}")
    private String img;

    @Override
    public String upLoad(MultipartFile file, String type) {
        // 文件名
        String fileName = file.getOriginalFilename();
        // 后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //新文件名
        String newFileName = UUID.randomUUID() + suffixName;
        //创建File
        try {
            FileUtil.uploadFile(file.getBytes(), img, newFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //创建返回的信息
        return IP + "/images/" + newFileName;
    }
}
